package logic

import (
	"context"
	"github.com/jinzhu/copier"
	"grpc-common/market/types/market"
	"market-api/internal/svc"
	"market-api/internal/types"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type MarketLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewMarketLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MarketLogic {
	return &MarketLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

/*
*
获取币种行情
*/
func (l *MarketLogic) SymbolThumbTrend(req *types.MarketReq) (list []*types.CoinThumbResp, err error) {
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	symbolThumbRes, err := l.svcCtx.MarketRpc.FindSymbolThumbTrend(ctx, &market.MarketReq{
		Ip: req.Ip,
	})
	if err != nil {
		return nil, err
	}
	if err = copier.Copy(&list, symbolThumbRes.List); err != nil {
		return nil, err
	}
	return
}

/*
*
获取币种行情2
*/
func (l *MarketLogic) SymbolThumb(req *types.MarketReq) (list []*types.CoinThumbResp, err error) {
	ctx, cancel := context.WithTimeout(l.ctx, 5*time.Second)
	defer cancel()
	symbolThumbRes, err := l.svcCtx.MarketRpc.FindSymbolThumbTrend(ctx, &market.MarketReq{
		Ip: req.Ip,
	})
	if err != nil {
		return nil, err
	}
	if err = copier.Copy(&list, symbolThumbRes.List); err != nil {
		return nil, err
	}
	return
}

/*
* 交易币详情
 */
func (l *MarketLogic) SymbolInfo(req *types.MarketReq) (*types.ExchangeCoinResp, error) {
	ctx, cancel := context.WithTimeout(l.ctx, 10*time.Second)
	defer cancel()
	//ExchangeCoin
	exchangeCoinInfo, err := l.svcCtx.MarketRpc.FindSymbolInfo(ctx, &market.MarketReq{
		Ip:     req.Ip,
		Symbol: req.Symbol,
	})
	if err != nil {
		return nil, err
	}
	resp := &types.ExchangeCoinResp{}
	if err = copier.Copy(&resp, exchangeCoinInfo); err != nil {
		return nil, err
	}
	return resp, err
}

/*
*
获取货币详情
*/
func (l *MarketLogic) CoinInfo(req *types.MarketReq) (*types.Coin, error) {
	ctx, cancel := context.WithTimeout(l.ctx, 10*time.Second)
	defer cancel()
	//coinInfo
	coinInfo, err := l.svcCtx.MarketRpc.FindCoinInfo(ctx, &market.MarketReq{
		Ip:   req.Ip,
		Unit: req.Unit,
	})
	if err != nil {
		return nil, err
	}
	coin := &types.Coin{}
	if err = copier.Copy(&coin, coinInfo); err != nil {
		return nil, err
	}
	return coin, err
}

/*
*
获取历史k线
*/
func (l *MarketLogic) History(req *types.MarketReq) (*types.HistoryKline, error) {
	ctx, cancel := context.WithTimeout(l.ctx, 10*time.Second)
	defer cancel()
	historyKline, err := l.svcCtx.MarketRpc.HistoryKline(ctx, &market.MarketReq{
		Symbol:     req.Symbol,
		From:       req.From,
		To:         req.To,
		Resolution: req.Resolution,
	})
	if err != nil {
		return nil, err
	}
	histories := historyKline.List
	var list = make([][]any, len(histories))
	for i, v := range histories {
		content := make([]any, 6)
		content[0] = v.Time
		content[1] = v.Open
		content[2] = v.High
		content[3] = v.Low
		content[4] = v.Close
		content[5] = v.Volume
		list[i] = content
	}
	return &types.HistoryKline{
		List: list,
	}, nil
}
